//
//  HMRecommendDetailTVC.m
//  HowToDo
//
//  Created by Stan on 2017-01-03.
//  Copyright © 2017 KY. All rights reserved.
//

#import "HMRecommendDetailVC.h"
#import "HMHomeRecommendDetailTVC.h"
#import "HMShoppingCarTool.h"
#import "MOPopWindow.h"
#import "HMRecommendPopView.h"
#import "HMPayViewController.h"


@interface HMRecommendDetailVC ()

@property(strong,nonatomic)HMHomeRecommendDetailTVC *tableController;

@property(strong,nonatomic)UIButton *addShoppingCartBtn;


@end

@implementation HMRecommendDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //    包一个tableView，用来展示推荐列表
    
    [self.view addSubview:self.tableController.tableView];
    //    用来修复tablView没有自动顶头的BUG
    self.tableController.tableView.frame = self.view.bounds;
    [self createFooterView];
    
    [self addChildViewController:self.tableController];
    
    
    //-------------设置导航栏开始--------------------------------
    [super setupNavRightItemsByTwo];
    //-------------设置导航栏结束--------------------------------
}

//在屏幕最下方的购物车
- (void)createFooterView{
    UIView *footerView = [[UIView alloc]init];
    [self.view addSubview:footerView];
    footerView.backgroundColor = [UIColor whiteColor];
    [footerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
    }];
    
    //    加入购物车的Btn
    UIButton *addShoppingCartBtn = [[UIButton alloc]init];
    self.addShoppingCartBtn = addShoppingCartBtn;
    [footerView addSubview:addShoppingCartBtn];
    addShoppingCartBtn.backgroundColor = [UIColor whiteColor];
    
    [addShoppingCartBtn hm_setTitle:@"加入购物车" andTiTleColorWithHex:0x686868 forState:UIControlStateNormal];
    addShoppingCartBtn.titleLabel.font = [UIFont systemFontOfSize:k_FONT_NORMAL];
    
    [addShoppingCartBtn addTarget:self action:@selector(shoppingCartBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    addShoppingCartBtn.layer.borderWidth = 1.0;
    addShoppingCartBtn.layer.borderColor = [UIColor colorWithHex:0xcbcbcb].CGColor;
    addShoppingCartBtn.layer.cornerRadius = 5.0;
    
    
    //    立刻购买的btn
    UIButton *buyBtn = [[UIButton alloc]init];
    [footerView addSubview:buyBtn];
    buyBtn.backgroundColor = [UIColor orangeColor];
    [buyBtn setTitle:@"购买" forState:UIControlStateNormal];
    [buyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    buyBtn.titleLabel.font = [UIFont systemFontOfSize:k_FONT_NORMAL];
    
    buyBtn.layer.cornerRadius = 5.0;
    
    [buyBtn addTarget:self action:@selector(buyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    //    购物车btn和立刻购买btn 等宽等高进行约束
    [addShoppingCartBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(6);
        make.left.offset(10);
        make.bottom.equalTo(footerView.mas_bottom).offset(-6);
        make.right.equalTo(buyBtn.mas_left).offset(-10);
        make.height.offset(40);
    }];
    
    [buyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(addShoppingCartBtn.mas_centerY).offset(0);
        make.right.offset(-10);
        make.height.equalTo(addShoppingCartBtn.mas_height).offset(0);
        make.width.equalTo(addShoppingCartBtn.mas_width).offset(0);
    }];
    
    [footerView layoutIfNeeded];
    footerView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, CGRectGetMaxY(addShoppingCartBtn.frame) + 6);
    
}


- (void)setResource_id:(NSString *)resource_id{
    _resource_id = resource_id;
    
    self.tableController.resource_id = resource_id;
    
}

//点击购买
- (void)buyBtnClick:(UIButton *)btn{
    
    HMRecommendPopView *popView = [HMRecommendPopView initWithFirstLabel:_recommendListModel.resource_name andSecondLabel:[NSString stringWithFormat:@"¥%@",_recommendListModel.price.description] andConfirmBlock:^{
        [SVProgressHUD showErrorWithStatus:@"余额不足，请充值"];
        [SVProgressHUD setMinimumDismissTimeInterval:0.1];

        [self mo_dissmissCurrentCompletion:nil];

        HMPayViewController *payVC = [[HMPayViewController alloc]init];
        [self.navigationController pushViewController:payVC animated:YES];
        
    } andCancelBlock:^{
        [self mo_dissmissCurrentCompletion:nil];
        
    }];
    //    HMRecommendPopView *popView = [[HMRecommendPopView alloc] initWithFirstLabel:_recommendListModel.resource_name andSecondLabel:[NSString stringWithFormat:@"¥%@",_recommendListModel.price.description] andConfirmBlock:^{
    //
    //
    //    } andCancelBlock:^{
    //
    //        [self mo_dissmissCurrentCompletion:nil];
    //    }];
    
    
    [self mo_presentContentView:popView option:BottomPop completion:nil];
}


//点击购物车
- (void)shoppingCartBtnClick:(UIButton *)btn
{
    if (btn.selected) {
        
        btn.selected = NO;
        
        [[HMShoppingCarTool sharedShoppingCarTool] removeShoppingCarDataWithID:_recommendListModel.resource_id];
        
        self.addShoppingCartBtn.backgroundColor = [UIColor whiteColor];
        
        [self.addShoppingCartBtn hm_setTitle:@"加入购物车" andTiTleColorWithHex:0x686868 forState:UIControlStateNormal];
        
        [SVProgressHUD showSuccessWithStatus:@"已从购物车移除"];
        [SVProgressHUD setMinimumDismissTimeInterval:0.1];
    }
    else
    {
        btn.selected = YES;
        
        [[HMShoppingCarTool sharedShoppingCarTool] getShoppingCarDataWithID:_recommendListModel.resource_id andImageUrl:_recommendListModel.image_url andName:_recommendListModel.resource_name andContent:_recommendListModel.resource_content andEnclosure:_recommendListModel.resource_enclosure andPrice:_recommendListModel.price.description];
        
        
        
        
        
        self.addShoppingCartBtn.backgroundColor = [UIColor lightGrayColor];
        [self.addShoppingCartBtn hm_setTitle:@"从购物车移除" andTiTleColorWithHex:0x686868 forState:UIControlStateNormal];
        [SVProgressHUD showSuccessWithStatus:@"成功加入购物车"];
        [SVProgressHUD setMinimumDismissTimeInterval:0.1];
    }
    
    
    
}

#pragma 懒加载
- (HMHomeRecommendDetailTVC *)tableController{
    if (_tableController == nil) {
        _tableController = [[HMHomeRecommendDetailTVC alloc]initWithStyle:UITableViewStyleGrouped];
    }
    return _tableController;
}
@end
